home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-06-05 | 2.2 KB | 92 lines | [MATS/MATL] |
- echo off;
- % NUMERICAL METHODS: MATLAB Programs, (c) John H. Mathews 1994
- % To accompany the text:
- % NUMERICAL METHODS for Mathematics, Science and Engineering, 2nd Ed, 1992
- % Prentice Hall, Englewood Cliffs, New Jersey, 07632, U.S.A.
- % This free software is complements of the author.
-
- % Algorithm 2.3 (False position or Regula Falsi Method).
- % Section 2.2, Bracketing Methods for Locating a Root, Page 62
- echo on; clc; format long; hold off; clear
- % This program implements the Regula Falsi method.
-
- % Define and store the function f(x) in the M-file f.m
- % function y = f(x)
- % y = x.*sin(x) - 1;
-
- delete f.m
- diary f.m; disp('function y = f(x)');...
- disp('y = x.*sin(x) - 1;');...
- diary off;
-
- % Remark. f.m and regula.m are used for Algorithm 2.3
- f(0); % Test for file f.m
- pause % Press any key to see the graph y = f(x).
-
- clc; clg;
- a = 0;
- b = 2;
- c = -1;
- d = 1;
- h = (b-a)/150;
- X = a:h:b;
- Y = f(X);
- axis([a b c d]);...
- plot(X,Y,'-g');...
- hold on;...
- plot([a b],[0 0],'b',[0 0],[c d],'b');...
- xlabel('x');...
- ylabel('y');...
- title('Graph of y = f(x).');...
- grid;...
- axis;...
- hold off;...
- shg; pause % Press any key to continue.
-
- clc;
- % Place the starting endpoints for [a,b] in a and b
-
- % Place the abscissa tolerance in delta
-
- % Place the ordinate tolerance in epsilon
-
- % Place the maximum number of iterations in max1
-
- a = 0;
- b = 2;
- delta = 1e-6;
- epsilon = 1e-6;
- max1 = 50;
-
- [p,yp,err,P] = regula('f',a,b,delta,epsilon,max1);
-
- pause % Press any key for the list of iterations.
-
- clc; clg;
- [m1 m2] = size(P);
- n0 = min(7,m1);
- Xa = P(1:n0,1); Xa = [a,Xa',b];
- Xb = P(1:n0,2); Xb = [a,Xb',b];
- Z0 = zeros(1,n0+2);
- axis([0 2 -1 1]);...
- plot(X,Y,'-g',Xa,Z0,'or',Xb,Z0,'or');...
- hold on;...
- plot([a b],[0 0],'b',[0 0],[c d],'b');...
- xlabel('x');...
- ylabel('y');...
- title('Graphical analysis for the Regula Falsi method.');...
- grid;...
- axis;...
- hold off;...
- shg; pause % Press any key to continue.
-
- Mx1 = 'Iterations for the Regula-Falsi method.';
- Mx2 = ' a b';
- Mx3 = 'The approximate root is:';
- Mx4 = 'The error estimate for p is ± ';
- clc,echo off, diary output,...
- disp(''),disp(Mx1),disp(''),disp(Mx2),disp(P),...
- disp(''),disp(Mx3),disp(''),disp('p = '),disp(p),...
- disp('f(p) = '),disp(yp),disp(''),...
- disp([Mx4,num2str(err)]),diary off, echo on
-